package com.ys.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ys.WebConstants;
import com.ys.extend.common.exception.BusinessRuntimeException;
import com.ys.extend.common.model.AjaxJson;
import com.ys.web.entity.User;
import com.ys.web.pojo.UserInfo;
import com.ys.web.service.UserService;

/**
 * 用户表 
 * @author wdj
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/userController")
public class UserController {
	
	@Autowired
	private UserService userService;
	
	private String message;
	
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	
	@RequestMapping(params="getSem")
	@ResponseBody
	public List<UserInfo> getSems(HttpServletRequest httpServletRequest){
		try {
			return userService.getAllSemUser();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
	}
	
	@RequestMapping(params="getUser")
	@ResponseBody
	public List<UserInfo> getUsers(int page){
		if(page==0){
			page=1;
		}
		
		try {
			return userService.getUserInfoPage(page);
		} catch (Exception e) {
			
			e.printStackTrace();
			return null;
		}
		
	}
	
	@RequestMapping(params="getAllUser")
	@ResponseBody
	public List<UserInfo> getAllUsers(){
		
		
		try {
			return userService.getAllUserInfo();
		} catch (Exception e) {
			
			e.printStackTrace();
			return null;
		}
		
	}
	
	@RequestMapping(params="saveUser")
	@ResponseBody
	public String saveUser(@RequestBody UserInfo userinfo){
		try {
			System.out.println("saveUser");
			if(userinfo.getId()!=null){
				userService.save(userinfo);
			}else{
				userService.add(userinfo);
			}
			
			return WebConstants.SUCCESS_CODE;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return WebConstants.FAIL_CODE;
		}
	}
	
	
	
	/**
	 * 添加
	 * @param user
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "save")
	@ResponseBody
	public AjaxJson save(User user, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		message = "添加成功";
		
		try {
			//userService.save(user);
		} catch (Exception e) {
			e.printStackTrace();
			message = "添加失败";
			j.setSuccess(false);
			throw new BusinessRuntimeException(e.getMessage());
		}
		
		j.setMsg(message);
		return j;	
	}
	
	/**
	 * 删除用户
	 * @param user
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "deleteUser")
	@ResponseBody
	public AjaxJson delete(Integer id, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		message = "删除成功";
		
		try {
			userService.delete(id);
			//userService.save(user);
		} catch (Exception e) {
			e.printStackTrace();
			message = "删除失败";
			j.setSuccess(false);
			throw new BusinessRuntimeException(e.getMessage());
		}
		
		j.setMsg(message);
		return j;	
	}
	
	
	@RequestMapping(params = "showUser")
	public ModelAndView showUser(HttpServletRequest request){
		return new ModelAndView("/user/user");
		
	}
	
	@RequestMapping(params ="isUserExist")
	@ResponseBody
	public String isUserExist(String username){
		try {
			if(userService.isUsernameExist(username)){
				return WebConstants.SUCCESS_CODE;
			}else{
				return WebConstants.FAIL_CODE;
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return WebConstants.FAIL_CODE;
		}
	}
	
	
	
}
